package com.tx.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @Author: tcx
 * @description: 文件上传下载工具类
 * @Date: 2019/11/10
 **/
public class FileUtil {

    /**
     * 生成文件上传的时间戳，用于文件保存在服务器的路径的一部分
     * @return
     */
    public static String createFileTimestamp(){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmss");
        String dateString = formatter.format(currentTime);
        return dateString;
        /*Date date = new Date();
        long time = date.getTime();
        return String.valueOf(time);*/
    }
     /* *
     * @description: 转换文件大小
     * @author tcx
     * @date 2019/11/10
     * @param size
     * @return java.lang.String
     */
    public static String readableFileSize(long size) {
        if (size <= 0) return "0";
        final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }
    /* *
     * @description:  删除文件
     * @author tcx
     * @date 2019/11/11
     * @param fileName
     * @return boolean
     */
    public static boolean deleteFile(String fileName){
        File file = new File(fileName);
        if(file.isFile() && file.exists()){
            Boolean succeedDelete = file.delete();
            if(succeedDelete){
                System.out.println("删除单个文件"+fileName+"成功！");
                return true;
            }
            else{
                System.out.println("删除单个文件"+fileName+"失败！");
                return true;
            }
        }else{
            System.out.println("删除单个文件"+fileName+"失败！");
            return false;
        }
    }
    /* *
     * @description:  获得文件类型
     * @author tcx
     * @date 2019/11/11
     * @param url
     * @return java.lang.String
     */
    public static String getType(String url){
        //获得文件类型
        String[] type = url.split("\\.");
        return type[type.length-1];
    }
    public static String getImage(String fType){
        String fImageUrl = "";
        switch (fType){
            case "zip":
                fImageUrl =ImageUrl.fImageZip;
                break;
            case "rar":
                fImageUrl =ImageUrl.fImageRAR;
                break;
            case "png":
                fImageUrl =ImageUrl.fImagePng;
                break;
            case "jpg":
            case "jpeg":
                fImageUrl =ImageUrl.fImageJpg;
                break;
            case "docx":
            case "doc":
                fImageUrl =ImageUrl.fImageWord;
                break;
            case "xls":
            case "xlsx":
                fImageUrl =ImageUrl.fImageExcel;
                break;
            case "ppt":
            case "pptx":
                fImageUrl =ImageUrl.fImagePPT;
                break;
            case "pdf":
                fImageUrl =ImageUrl.fImagePDF;
                break;
            case "txt":
                fImageUrl =ImageUrl.fImageTXT;
                break;
            default:
                fImageUrl =ImageUrl.fImageWeiZhi;
        }
        return fImageUrl;
    }
    /* *
     * @description:  上传头像
     * @author tcx
     * @date 2019/11/15
     * @param file
     * @param fileUrl
     * @param fName
     * @return java.lang.String
     */
    public static String uploadHeadImage(MultipartFile file, String fileUrl, String fName) throws IOException {
        //在根路径下创建文件夹
        File f = new File(fileUrl+"/headimage");
        if(!f.exists()){//如果文件夹不存在
            f.mkdir();//创建文件夹
        }
        //生成时间戳
        String time = createFileTimestamp();
        //生成6位随机数
        String salt = salt();
        //后缀名
        String houZhui = getType(fName);
        FileOutputStream fos = new FileOutputStream(new File(fileUrl+"headimage",time+salt+"."+houZhui));
        fos.write(file.getBytes());
        fos.flush();
        fos.close();
        return "/headimage/"+time+salt+"."+houZhui;
    }
    /* *
     * @description:  上传文件
     * @author tcx
     * @date 2019/11/09
     * @param file
     * @param fileUrl
     * @param fName
     * @return java.lang.String
     */
    public static String writeFileToUrl(MultipartFile file, String fileUrl, String fName) throws IOException {
        //在根路径下创建文件夹
        File f = new File(fileUrl+"/upload");
        if(!f.exists()){//如果文件夹不存在
            f.mkdir();//创建文件夹
        }
        //生成时间戳
        String time = createFileTimestamp();
        //生成6位随机数
        String salt = salt();
        //后缀名
        String houZhui = getType(fName);
        FileOutputStream fos = new FileOutputStream(new File(fileUrl+"upload",time+salt+"."+houZhui));
        fos.write(file.getBytes());
        fos.flush();
        fos.close();
        return fileUrl+"upload/"+time+salt+"."+houZhui;
    }
    /**
     * 生成6位随机数
     */
    public static String salt(){
        Random r = new Random();
        StringBuilder sb = new StringBuilder(6);
        sb.append(r.nextInt(99999999)).append(r.nextInt(99999999));
        int len = sb.length();
        if (len < 6) {
            for (int i = 0; i < 6 - len; i++) {
                sb.append("0");
            }
        }
        String salt = sb.toString();
        return  salt;
    }

}
